﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Site_Default : BaseSitePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer }; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSite.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Site>>(dsSite_SelectData);
        dsMappings.UpdatingData += new EventHandler<Acms.DataControls.UpdatingDataEventArgs<SiteMapping>>(dsMappings_UpdatingData);
        dsMappings.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.SiteMapping>>(dsMappings_SelectData);
        btnInsert.Click += new EventHandler(btnInsert_Click);
        btnEdit.Click += new EventHandler(btnEdit_Click);
        btnSave.Click += new EventHandler(btnSave_Click);
        btnCancel.Click += new EventHandler(btnCancel_Click);
        dvSite.ItemUpdated += new DetailsViewUpdatedEventHandler(dvSite_ItemUpdated);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    void dsMappings_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<SiteMapping> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.SiteId = AcmsContext.SiteId;
        }
    }

    void dvSite_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
        }
        if (e.Exception == null || e.ExceptionHandled)
        {
            btnEdit.Visible = true;
            btnSave.Visible = btnCancel.Visible = false;
        }
    }

    void btnCancel_Click(object sender, EventArgs e)
    {
        dvSite.ChangeMode(DetailsViewMode.ReadOnly);

        btnEdit.Visible = true;
        btnSave.Visible = btnCancel.Visible = false;
    }

    void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            dvSite.UpdateItem(true);
        }
    }

    void btnEdit_Click(object sender, EventArgs e)
    {
        bool rebingMappings = false;
        if (dvMappings.EditIndexes.Count > 0)
        {
            dvMappings.EditIndexes.Clear(); 
            rebingMappings = true;
        }
        if (dvMappings.MasterTableView.IsItemInserted)
        {
            dvMappings.MasterTableView.IsItemInserted = false;
            rebingMappings = true;
        }
        if (rebingMappings)
        {
            dvMappings.Rebind();
        }
        btnEdit.Visible = false;
        btnSave.Visible = btnCancel.Visible = true;
        dvSite.ChangeMode(DetailsViewMode.Edit);
    }

    void btnInsert_Click(object sender, EventArgs e)
    {
        dvMappings.EditIndexes.Clear();
        dvMappings.MasterTableView.IsItemInserted = true;
        dvMappings.Rebind();
    }

    void dsMappings_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.SiteMapping> e)
    {
        e.SetData(SiteMapping.GetAllBySiteId(AcmsContext.SiteId));
    }

    void dsSite_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Site> e)
    {
        e.SetData(Acms.Bll.Site.GetById(AcmsContext.SiteId));
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Site Settings", this.AcmsContext.OriginalUrl);
    }
}
